package test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * 
 * use jdk dynomic proxy, the target object must have a interface
 * when the target object was invoked,
 * the invoked method which was declared in the interface
 * that will be invoked the method of invoke on class dynomicProxy  automitc.2
 *
 */
public class DynomicProxy implements InvocationHandler{
	
	private Object targetObject;
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("proxy invokde before");
		method.invoke(this.targetObject, args);
		System.out.println("proxy invokde after");
		return null;
	}
	/**
	 * @return the targetObject
	 */
	public Object getTargetObject() {
		return targetObject;
	}

	/**
	 * @param targetObject the targetObject to set
	 */
	public void setTargetObject(Object targetObject) {
		this.targetObject = targetObject;
	}

	public Object getProxy(){
		return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), this.targetObject.getClass().getInterfaces(), this);
	}
	
	public static void main(String[] args) {
		DynomicProxy p = new DynomicProxy();
		TargetobjI targetObject = new TargetObj();
		p.setTargetObject(targetObject);
		TargetobjI t = (TargetobjI)p.getProxy();
		t.m();
	}
}
